package com.wj.jiangan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.wj.jiangan.mapper.OrdermxMapper;
import com.wj.jiangan.pojo.Ordermx;
import com.wj.jiangan.service.IOrdermxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 订单详情服务实现类
 * </p>
 *
 * @author wj
 * @since 2019-11-05
 */
@Service
public class OrdersmxServiceImpl implements IOrdermxService {


    @Autowired
    private OrdermxMapper ordermxMapper;


    @Override
    public int addOrdermx(Ordermx ordermx) {
        QueryWrapper<Ordermx> qw = new QueryWrapper<>();
        qw.select("x_count", "r_id").eq("r_id", ordermx.getRId())
                .eq("p_id", ordermx.getPId())
                .eq("y_id", ordermx.getYId());

        Ordermx od = ordermxMapper.selectOne(qw);
        //订单中有该商品就更新该商品数据
        if (od != null && od.getXCount() != null) {
            UpdateWrapper<Ordermx> uw = new UpdateWrapper<>();
            uw.set("x_count", ordermx.getXCount() + od.getXCount())
                    .set("x_price", ordermx.getXPrice())
                    .eq("p_id", ordermx.getPId())
                    .eq("y_id", ordermx.getYId());
            return ordermxMapper.update(ordermx, uw);
        } else {
            return ordermxMapper.insert(ordermx);
        }
    }

    @Override
    public int deleteOrdermxByXid(Integer xid) {
        return ordermxMapper.deleteById(xid);
    }
}
